<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/23
 * Time: 11:22
 */

namespace App\Process;


use App\Enums\UrlEnums;
use App\Library\Auth;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\Component\Timer;
use EasySwoole\HttpClient\HttpClient;

/**
 * 通过对局即时比分更新正在比赛的状态
 * Class CheckMatchStatus
 * @package App\Process
 */
class CheckMatchStatus extends AbstractProcess
{
    public function run($arg)
    {
        go(function ()use($arg){
            Timer::getInstance()->loop(8000,function ()use($arg){
                $table = $arg['table'];
                $path  = $arg['path'];
                MysqlPool::invoke(function (MysqlObject $db)use ($table,$path){
                    $matchs = $db->whereIn('status',[0,1,3])->get($table);
                    list($s1, $s2) = explode(' ', microtime());
                    $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
                    foreach ( $matchs as $match ){
                        if( $microtime >= $match['start_time'] ) { //更新游戏状态
                            $url = UrlEnums::FJURL.$path.'?version=2';
                            $client = new HttpClient($url);
                            $option = [
                                'path'      => $path,
                                'api_time'  => $microtime
                            ];
                            $header = Auth::getInstance()->header($option);
                            $client->setHeaders($header,true,false);
                            $response = $client->get();
                            $content = $response->getBody();
                            $data = json_decode($content,true);
                            if( isset($data['code']) && $data['code'] == 200 && $data['data'] ){
                                foreach ( $data['data'] as $k=>$v ){
                                    $status = $v['status'];
                                    $battle_ids = json_encode($v['battle_ids']);
                                    $team_a_score = $v['team_a_score'];
                                    $team_b_score = $v['team_b_score'];
                                    $udata = [
                                        'status'     => $status,
                                        'battle_ids' => $battle_ids,
                                        'team_a_score'=> $team_a_score,
                                        'team_b_score'=> $team_b_score
                                    ];
                                    if( isset($v['map_bp']) ){
                                        $udata['map_bp'] = json_encode($v['map_bp']);
                                    }
                                    if( isset($v['map_names']) ){
                                        $udata['map_names'] = json_encode($v['map_names']);
                                    }
                                    if( isset($v['match_time']) ){
                                        $udata['match_time'] = $v['match_time'];
                                        $udata['start_time'] = $v['match_time'];
                                    }
                                    $res = $db->where('match_id',$v['match_id'])->update($table,$udata);
                                }
                            }
                        }
                    }
                });
            });
        });
    }
}